package com.spider.admin.controller;

import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.logs.model.LogDocument;
import com.spider.admin.logs.service.LogIndexService;
import com.spider.admin.logs.service.LogSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/logs")
public class LogController {
    @Autowired
    private LogIndexService logIndexService;

    @Autowired
    private LogSettingService logSettingService;

    @PostMapping("/add")
    public void storeLogs(@RequestBody List<LogDocument> logList) throws SpiderException {
        logIndexService.insertLogsIndex(logList);
    }

    @GetMapping("/search")
    public PageResult<LogDocument> searchLogs
            (@RequestParam(required = false) String appName,
             @RequestParam(required = false) String serviceName,
             @RequestParam(required = false) Integer page,
             @RequestParam(required = false) Integer pageSize,
             @RequestParam(required = false) String startCallDate,
             @RequestParam(required = false) String endCallDate) throws SpiderException {
        return logIndexService.searchLogs(null, null, page, pageSize, appName, serviceName, startCallDate, endCallDate);
    }

    @GetMapping("/searchForPub/{tenantId}")
    public PageResult<LogDocument> searchLogsForPub
            (@PathVariable String tenantId,
             @RequestParam(required = false) String appName,
             @RequestParam(required = false) String serviceName,
             @RequestParam(required = false) Integer page,
             @RequestParam(required = false) Integer pageSize,
             @RequestParam(required = false) String startCallDate,
             @RequestParam(required = false) String endCallDate) throws SpiderException {
        return logIndexService.searchLogs(tenantId, null, page, pageSize, appName, serviceName, startCallDate, endCallDate);
    }

    @GetMapping("/searchForCall/{tenantId}")
    public PageResult<LogDocument> searchLogsForCall
            (@PathVariable String tenantId,
             @RequestParam(required = false) String appName,
             @RequestParam(required = false) String serviceName,
             @RequestParam(required = false) Integer page,
             @RequestParam(required = false) Integer pageSize,
             @RequestParam(required = false) String startCallDate,
             @RequestParam(required = false) String endCallDate) throws SpiderException {
        return logIndexService.searchLogs(null, tenantId, page, pageSize, appName, serviceName, startCallDate, endCallDate);
    }

    @GetMapping("/get/{docId}")
    public LogDocument getLogDoc(@PathVariable String docId) throws SpiderException {
//        return logService.getLogDoc(docId);
        return null;
    }

    @GetMapping("/settings/{serviceId}")
    public Map<String, String> getLogSettingByServiceId(@PathVariable String serviceId) throws SpiderException {
        return logSettingService.getLogSettingsByServiceId(serviceId);
    }

    @PutMapping("/settings/update/{serviceId}")
    public boolean updateLogSettings(@PathVariable String serviceId, @RequestBody Map<String, String> settings) throws SpiderException {
        return logSettingService.updateLogSettings(serviceId, settings);
    }
}
